<template>
    <div>
        <el-form :model="addSubject">

           <el-form-item label="学科" :label-width="formLabelWidth">
                   <el-input v-model="addSubject.subject" clearable ></el-input>
           </el-form-item>
           <el-form-item label="年级" :label-width="formLabelWidth">
               <el-select v-model="addSubject.grade" placeholder="请选择" clearable>
                 <el-option
                   v-for="item in grad"
                   :value="item.dictValue" 
                   :key="item.dictValue"             
                   :label="item.dictLabel">
                 </el-option>
               </el-select>

               <br>
               <br>
               <el-button @click="adminNull()">重 置</el-button>
               <el-button  v-if="this.id ==null" type="primary" @click="addTbAdmin()">提交</el-button>
               <el-button v-else type="primary" @click="upTbAdmin()">提 交</el-button>
           </el-form-item>
           
             


           </el-form> 
    </div>
</template>

<script>
import axios from 'axios';

export default {
    data(){
        return{
            id:'',
            grad:[],
            formLabelWidth: '120px', //新增表单宽度
            addSubject:{
                "grade": "",
          
                "subject": ""
            },
           

        }
    },
    created(){
        this.id = sessionStorage.getItem("id")
        console.log(this.id);
        this.searchDict()

        if(sessionStorage.getItem("id")){
            this.initGetAdminById()
        }

    },
    methods:{

        

        //回显
        initGetAdminById(){
            axios.get("/api/subject/findSubjectById?id="+this.id).then(res=>{
                this.addSubject = res.data.data
                sessionStorage.removeItem("id")
            })

        },

        //新增
        addTbAdmin(){
            axios.post("/api/subject/addSubject",this.addSubject).then(res=>{
                //弹框
                this.$message({
                    message: res.data.message,
                    type: 'success'
                    });




            })
        },
        //修改
        upTbAdmin(){
            axios.put("/api/subject/updateSubject",this.addSubject).then(res=>{
                //弹框
                this.$message({
                    message: res.data.message,
                    type: 'success'
                    });
                    //跳页面
                    // this.$router.push("/adminShow")
            })


        },

       //查询字典数据的性别
       searchDict(){
         
          axios.get("/api/dict/findDictDataByDictTypeIdSysUserSex?dictType=sys_grade_status").then(res=>{
            this.grad =res.data.data
          })

        },
        //重置
        adminNull(){
            this.addadmin ={}
        }

    }
    
}
</script>

<style scoped>

</style>